package com.bytedance.frameworks.baselib.network.http.util;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Process;

import com.bytedance.common.utility.Logger;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Iterator;

public final class ProcessUtils {
    private static String a;

    static {
        ProcessUtils.a = null;
    }

    private static String a() {
        BufferedReader v1_1 = null;
        try {
            v1_1 = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/" + Process.myPid() + "/cmdline"), "iso-8859-1"));
            StringBuilder v2_1 = new StringBuilder();
            while (true) {
                int v3 = v1_1.read();
                if (v3 <= 0) {
                    break;
                }
                v2_1.append(((char) v3));
            }

            if (Logger.debug()) {
                Logger.d("Process", "get processName = " + v2_1.toString());
            }

            return v2_1.toString();
        } catch (Throwable v0_1) {
            v0_1.printStackTrace();
        } finally {
            if (v1_1 != null) {
                try {
                    v1_1.close();
                } catch (Exception v1_2) {
                }
            }
        }
        return null;
    }

    public static boolean a(Context arg2) {
        String v0 = ProcessUtils.c(arg2);
        boolean v0_1 = v0 == null || !v0.endsWith(":push") ? false : true;
        return v0_1;
    }

    public static boolean b(Context arg3) {
        boolean v0 = false;
        String v1 = ProcessUtils.c(arg3);
        if ((v1 == null || !v1.contains(":")) && (v1 != null && (v1.equals(arg3.getPackageName())))) {
            v0 = true;
        }

        return v0;
    }

    private static String c(Context arg4) {
        Object v0_2;
        String v0 = ProcessUtils.a;
        if (android.support.design.a.isTextEmpty(v0)) {
            try {
                int v1 = Process.myPid();
                Iterator v2 = ((ActivityManager) arg4.getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses().iterator();
                do {
                    if (!v2.hasNext()) {
                        v0 = ProcessUtils.a();
                        ProcessUtils.a = v0;
                        return v0;
                    }

                    v0_2 = v2.next();
                } while (((ActivityManager.RunningAppProcessInfo) v0_2).pid != v1);

                if (Logger.debug()) {
                    Logger.d("Process", "processName = " + ((ActivityManager.RunningAppProcessInfo) v0_2).processName);
                }

                v0 = ((ActivityManager.RunningAppProcessInfo) v0_2).processName;
                ProcessUtils.a = v0;
                return v0;
            } catch (Exception v0_1) {
                v0_1.printStackTrace();
            }

            label_29:
            v0 = ProcessUtils.a();
            ProcessUtils.a = v0;
        }

        return v0;
    }
}

